/**
 * Decrypt.java
 */
package com.vcsoft.emailresponder.util;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * 
 * @author bhosalevishal@gmail.com
 * @version 03-Sept-2012
 *
 */
public class Decrypt {
	
	// iv length should be 16 bytes
	private static final String IV = "fedcba9876543210";
	private String key = null;
	private Cipher cipher = null;
	private SecretKeySpec keySpec = null;
	private IvParameterSpec ivSpec = null;

	/**
	 * public constructor
	 * @param key
	 * @throws Exception
	 */
	public Decrypt(String key) throws Exception {
		this.key = key;

		// Make sure the key length should be 16
		int len = this.key.length();
		if (len < 16) {
			int addSpaces = 16 - len;
			for (int i = 0; i < addSpaces; i++) {
				this.key = this.key + " ";
			}
		} else {
			this.key = this.key.substring(0, 16);
		}
		this.keySpec = new SecretKeySpec(this.key.getBytes(), "AES");
		this.ivSpec = new IvParameterSpec(IV.getBytes());
		this.cipher = Cipher.getInstance("AES/CBC/NoPadding");
	}

	/**
	 * converts hexadecimal string into byte array
	 * @param str hexadecimal string
	 * @return byte array
	 */
	public byte[] hexToBytes(String str) {
		if (str == null) {
			return null;
		} else if (str.length() < 2) {
			return null;
		} else {
			int len = str.length() / 2;
			byte[] buffer = new byte[len];
			for (int i = 0; i < len; i++) {
				buffer[i] = (byte) Integer.parseInt(
						str.substring(i * 2, i * 2 + 2), 16);
			}
			return buffer;
		}
	}

	/**
	 * decryption of the encrypted string
	 * @param encrData
	 * @return String decrypted string
	 * @throws Exception
	 */
	public String decrypt(String encrData) throws Exception {
		this.cipher.init(Cipher.DECRYPT_MODE, this.keySpec, this.ivSpec);
		byte[] outText = this.cipher.doFinal(hexToBytes(encrData));

		String decrData = new String(outText).trim();
		return decrData;
	}
}